package com.purong.purong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.PuRongApp;
import com.purong.purong.R;
import com.purong.purong.bean.IdentifyBean;
import com.purong.purong.bean.LoginBean;
import com.yao.sdk.net.ResponseInfo;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by wenjiezeng on 2017/10/9.
 */
public class RealIdentifyActivity extends BaseActivity{
    private EditText identityEd,nameEd;
    private boolean identityInput,nameInput;
    private TextView confirm;

    @Override
    public int getRootViewId() {
        return R.layout.activity_real_identify;
    }

    public void initView(){
        showBack();
        showTitle("实名认证");
        identityEd=(EditText)findViewById(R.id.identity_ed);
        nameEd=(EditText)findViewById(R.id.name_ed);
        confirm=(TextView)findViewById(R.id.confirm);
        nameEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    nameInput = false;
                } else {
                    nameInput = true;
                }
                if (nameInput && identityInput) {
                    confirm.setBackgroundResource(R.drawable.button_blue_radius5);
                } else {
                    confirm.setBackgroundResource(R.drawable.button_gray_radius5);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        identityEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    identityInput = false;
                } else {
                    identityInput = true;
                }
                if (nameInput && identityInput) {
                    confirm.setBackgroundResource(R.drawable.button_blue_radius5);
                } else {
                    confirm.setBackgroundResource(R.drawable.button_gray_radius5);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void initData() {

    }

    private void identity(String name,String identity){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("real",name);
        params.put("identity",identity);
        request(Contant.SET_REAL, params, true,true);

    }
    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if(hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.SET_REAL)){
            if (responseInfo.isRequestOk){
                try{
                    IdentifyBean bean=JSON.parseObject(responseInfo.content,IdentifyBean.class);
                    if (bean!=null){
                        if (PuRongApp.getLoginInfo(this)!=null){
                            LoginBean loginBean=PuRongApp.getLoginInfo(this);
                            loginBean.setIs_real("2");
                            loginBean.setReal(bean.getReal());
                            loginBean.setIdentity(bean.getIdentity());
                            loginBean.save(this);

                        }
                        Intent intent=new Intent(this,RealInfoActivity.class);
                        intent.putExtra("IDENTIFY",bean.getIdentity());
                        intent.putExtra("NAME",bean.getReal());
                        startActivity(intent);
                        this.finish();
                    }
                }catch (Exception e){

                }

                Log.e("zwj","content="+responseInfo.content);
            }
        }
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.confirm:
                if (!nameInput){
                    showToast("请填写真实姓名");
                    return;
                }
                if (!identityInput){
                    showToast("请输入身份证号");
                    return;
                }
                identity(nameEd.getText().toString().trim(),identityEd.getText().toString().trim());
                break;
        }
    }
}
